/*
 * ConnectionListener.java
 *
 * Created on 28 de Junho de 2005, 22:28
 */

package jhub.conn;
import java.text.ParseException;
import java.io.*;
import java.net.*;
import java.util.Date;
import jhub.*;
import jhub.conn.exception.BadPasswdException;
import jhub.conn.exception.LoginErrorException;
import jhub.conn.exception.UnknownProtocolException;
import jhub.conn.exception.ValidateDeniedException;
import jhub.text.DCRawCommand;
import jhub.users.Admin;
import jhub.users.Operator;
import jhub.users.Registered;
import jhub.users.VIP;

/**
 *
 * @author  botelhodaniel
 */
public class HubListener implements Runnable{
    
    private boolean online;
    private HubSession hubSession;
    private ServerSocket connListener;
    
    
    /** Creates a new instance of ConnectionListener */
    public HubListener(HubSession hubSession) {
        this.online = true;
        this.hubSession = hubSession;
    }
    public void stop() throws IOException{
        this.online = false;
        connListener.close();
    }
    
    public void run() {
        
        try{
            connListener = new ServerSocket(hubSession.getHub().getPort());
            this.hubSession.setStartDate(new Date());
        }catch(IOException e){
            e.printStackTrace();
            return;
        }
        while(online){
            try {
                new Thread(new HandshakeHandler(this.hubSession,new UserSession(this.hubSession,connListener.accept()))).start();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    
}
